import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:thal/backstage/toolkit/utilities/color_palette.dart';

/// Toast提示管理类
/// 用于显示各种轻量级提示信息，所有提示都显示在屏幕上方
///
/// 使用示例：
/// ```dart
/// // 成功提示 - 蓝色
/// StageWhisper.cheer('Operation Successful!');
///
/// // 失败提示 - 红色
/// StageWhisper.lament('Operation Failed!');
///
/// // 警告提示 - 粉色
/// StageWhisper.caution('Warning Message');
///
/// // 信息提示 - 浅蓝色
/// StageWhisper.broadcast('Information');
/// ```
class StageWhisper {
  /// 私有构造函数，防止实例化
  StageWhisper._();

  /// 展示成功提示 - 蓝色
  static void cheer(String message, {Duration? duration}) {
    _showToastWithColor(
      color: ColorPalette.blue1,
      showMethod:
          () => EasyLoading.showSuccess(
            message,
            duration: duration ?? const Duration(seconds: 2),
            dismissOnTap: true,
            maskType: EasyLoadingMaskType.none,
          ),
      duration: duration ?? const Duration(seconds: 2),
    );
  }

  /// 展示失败提示 - 红色
  static void lament(String message, {Duration? duration}) {
    _showToastWithColor(
      color: ColorPalette.red,
      showMethod:
          () => EasyLoading.showError(
            message,
            duration: duration ?? const Duration(seconds: 2),
            dismissOnTap: true,
            maskType: EasyLoadingMaskType.none,
          ),
      duration: duration ?? const Duration(seconds: 2),
    );
  }

  /// 展示警告提示 - 粉色
  static void caution(String message, {Duration? duration}) {
    _showToastWithColor(
      color: ColorPalette.pink,
      showMethod:
          () => EasyLoading.showInfo(
            message,
            duration: duration ?? const Duration(seconds: 2),
            dismissOnTap: true,
            maskType: EasyLoadingMaskType.none,
          ),
      duration: duration ?? const Duration(seconds: 2),
    );
  }

  /// 展示信息提示 - 浅蓝色
  static void broadcast(String message, {Duration? duration}) {
    _showToastWithColor(
      color: ColorPalette.blue2,
      showMethod:
          () => EasyLoading.showInfo(
            message,
            duration: duration ?? const Duration(seconds: 2),
            dismissOnTap: true,
            maskType: EasyLoadingMaskType.none,
          ),
      duration: duration ?? const Duration(seconds: 2),
    );
  }

  /// 内部方法：临时设置颜色并显示Toast，之后恢复原始颜色
  static void _showToastWithColor({
    required Color color,
    required Function showMethod,
    required Duration duration,
  }) {
    // 保存原始颜色
    final originalColor = EasyLoading.instance.indicatorColor;

    // 设置临时颜色
    EasyLoading.instance.indicatorColor = color;

    // 显示Toast
    showMethod();

    // Toast消失后恢复原始颜色
    Future.delayed(duration, () {
      EasyLoading.instance.indicatorColor = originalColor;
    });
  }

  /// 立即隐藏所有提示
  static void silence() {
    EasyLoading.dismiss();
  }

  /// 判断是否正在显示
  static bool get isSpeaking => EasyLoading.isShow;
}
